/*
 ImageBrowser
 
 Copyright (c) 2007-2008 Mary Jane Soft - Marlon J. Manrique
 	
 http://mjs.darkgreenmedia.com
 http://marlonj.darkgreenmedia.com
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 $Id: ImageBrowser.pde 375 2008-02-28 14:39:27Z marlonj $
 	
*/

/**
 * ImageBrowser
 *
 * This class show a list of images in a row of images, where the user can 
 * navigate using the LEFT and RIGTH keys
 */
class ImageBrowser
{  
  // Parent MIDlet
  PMIDlet pMIDlet;

  // Network client to retrieve thumbnails
  PClient imageClient;
  PRequest imageRequest;

  // Number of images
  int numImages;
  int loadCounter;
  int currentPage = 1;

  // The images
  PImage[] images;
  String[] urls;

  // Current image
  int currentImage = -1;

  // Location
  int x;
  int y;  
  int currentX;
  int currentY;  

  // Image Size
  int imageWidth = 130;
  int imageHeight = 97;  

  // Loading Image
  PImage loadingImage;

  /**
   * Create the Image Browser
   *
   * @param pMIDlet the parent MIDlet
   */
  ImageBrowser(PMIDlet pMIDlet)
  {
    // Init elements
    this.pMIDlet = pMIDlet;    
    loadingImage = loadImage("loading.png");
  }

  /**
   * Draw the images
   */
  void draw()
  {    
    // Viewer position
    int tmpX = 0;
    int tmpY = 0;

    // If the images are available
    if(images != null)
    {
      // Draw each image
      for(int i=0; i<numImages; i++)
      {
        // If the image is not available         
        // put the loading image
        if(images[i] == null)
          images[i] = loadingImage;

        // Get the image
        PImage pImage = images[i];          
        tmpY = - pImage.height/2;

        // If the image is the current one
        // Draw a green rect over the image
        // and set current position 
        if(i == currentImage)
        {
          noFill();
          stroke(0,255,0);
          rect(x+tmpX-pImage.width/2-2,y+tmpY-2,pImage.width+4,pImage.height+4);
          currentX = tmpX;
        }

        // Draw the image
        image(pImage,x+tmpX-pImage.width/2,y+tmpY);

        // Update the position of the next photo 
        tmpX += imageWidth + 8;     
      }
    }

    // If the x value is not equal to the current
    // move the position of the set each time to center the
    // current photo
    if(x > -currentX) x -= 5;
    if(x < -currentX) x += 5;    
  }

  /**
   * Navigate through the photo set
   */
  void keyPressed()
  {
    switch(keyCode)
    {
      // If go the left
    case LEFT : 
      currentImage--;

      // If current photo is out of the set
      if(currentImage < 0)
        currentImage = 0;

      break;

      // If go the right
    case RIGHT :
      currentImage++;

      // If current photo is out of the set, load the next page
      if(currentImage >= numImages)
        currentImage = numImages - 1;
      break;    
    }
  }    


  /**
   * Listen library events
   */
  void libraryEvent(Object library, int event, Object data)
  {
    if(library == imageRequest)
    {
      // Get the photo data
      if(event == PRequest.EVENT_CONNECTED)
        imageRequest.readBytes();
      // Set the photo in the current set
      else if(event == PRequest.EVENT_DONE)
        addImage(loadImage((byte[])data));
      // If any error
      else if(event == PRequest.EVENT_ERROR)
        message = "Error : " + data;
    }    
  }

  /**
   * Add the image specified to the current list
   *
   * @param pImage Image to add
   */
  void addImage(PImage pImage)
  {
    images[loadCounter] = pImage;
    loadCounter++;

    if(loadCounter != numImages)
      downloadImage();
    else
      message = "Play Video";
  }

  /**
   * Load the images specified in the list of urls
   *
   * @param urls List of images urls
   */
  void downloadImages(String[] urls)
  {
    // Clear current list
    clear();
    message = "Loading Images ...";

    // Set current list 
    this.urls = urls;

    // Update info and create new array
    numImages = length(urls);
    images = new PImage[numImages];

    // Start downloading images
    downloadImage();
  }

  /**
   * Download next image
   */
  void downloadImage()
  {
    downloadImage(urls[loadCounter]);
  }

  /**
   * Download the image in the url
   *
   * @param url The image url
   */
  void downloadImage(String url)
  {
    // Split the url in server and file
    int index = url.indexOf('/',7);
    String server = url.substring(7,index);
    String file = url.substring(index);

    // Download the image 
    downloadImage(server,file);
  }

  /**
   * Download the image in the server and the resource
   *
   * @param server Server name 
   * @param file resource path
   */
  void downloadImage(String server, String file)
  {    
    // Request the photo to the server
    imageClient = new PClient(pMIDlet,server);
    imageRequest = imageClient.GET(file);
  }

  /**
   * Clear the list elements and attributes
   */
  void clear()
  {
    numImages = 0;    
    currentImage = 0;
    loadCounter = 0;
    x = currentX = 0;
    y = currentY = 0;
  }
}
